﻿/* Open source distributed without warranties of any kind. */

namespace Transcriber.Plugins
{
    #region Using
    using System;
    using System.IO;
    using System.Windows.Forms;
    #endregion

    /// <summary>
    /// Settings dialog for <see cref="RulesFileTranscriber"/>.
    /// </summary>
    public partial class frmRulesFileTranscriberSettings : Form
    {
        private RulesFileTranscriber mTranscriber;

        /// <summary>
        /// Initializes a new instance of the <see cref="frmRulesFileTranscriberSettings"/> class.
        /// </summary>
        /// <param name="transcriber">The <see cref="RulesFileTranscriber"/>.</param>
        public frmRulesFileTranscriberSettings(RulesFileTranscriber transcriber)
        {
            InitializeComponent();
            this.mTranscriber = transcriber;
        }

        /// <summary>
        /// Handles the Load event of the frmRulesFileTranscriberSettings control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void frmRulesFileTranscriberSettings_Load(object sender, EventArgs e)
        {
            foreach (FileInfo file in Rules.RulesDirectory.GetFiles("*.xml"))
            {
                try
                {
                    Rules rules = Rules.Load(file);

                    if (rules.DisplayInUserInterface)
                    {
                        string name = rules.Title;
                        int index = this.lstRules.Items.Add(rules);

                        if (mTranscriber.CurrentRules == file.Name)
                        {
                            this.lstRules.SelectedIndex = index;
                        }
                    }
                }
                catch (Exception x)
                {
                    MessageBox.Show(x.Message, "Error Loading Rules");
                }
            }
        }

        /// <summary>
        /// Handles the FormClosing event of the frmRulesFileTranscriberSettings control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Forms.FormClosingEventArgs"/> instance containing the event data.</param>
        private void frmRulesFileTranscriberSettings_FormClosing(object sender, FormClosingEventArgs e)
        {
            Rules rules = (Rules)this.lstRules.SelectedItem;

            if (null != rules)
            {
                this.mTranscriber.CurrentRules = rules.LoadedFrom;
            }
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnConvertNumber_Click(object sender, EventArgs e)
        {
            using (frmNumericBaseConverter dialog = new frmNumericBaseConverter())
            {
                dialog.ShowDialog();
            }
        }

        private void btnTestRegex_Click(object sender, EventArgs e)
        {
            using (frmRegexTest dialog = new frmRegexTest())
            {
                dialog.ShowDialog();
            }
        }
    }
}